#include <NoteRenderer.h>

namespace pp
{
    NoteRenderer::NoteRenderer()
    {
    }

    bool NoteRenderer::build(const ne::MIDISequencePtr pSequnce)
    {
        destroy();

        if (pSequnce.isNull()) return false;
        for (size_t i=0; i<pSequnce->getNumTracks(); ++i)
        {
            ne::MIDITrack *pTrack = pSequnce->getTrack(i);
            if (pTrack && pTrack->hasNotes())
            {
                NoteTrackRenderer *pRenderer = new NoteTrackRenderer();
                pRenderer->build(pTrack);
                int c = pTrack->getInstrument() % 12;
                pRenderer->setColor(NoteTrackColor[c]);
                mNoteTrackRendererList.push_back(pRenderer);
            }
        }

        return true;
    }

    bool NoteRenderer::update(const double startTicks, const double endTicks, const double deltaY)
    {
        for (NoteTrackRendererListConstIterator it=mNoteTrackRendererList.begin(); it!=mNoteTrackRendererList.end(); ++it)
        {
            (*it)->update(startTicks, endTicks, deltaY);
        }
        return true;
    }

    bool NoteRenderer::render()
    {
        for (NoteTrackRendererListConstIterator it=mNoteTrackRendererList.begin(); it!=mNoteTrackRendererList.end(); ++it)
        {
            (*it)->render();
        }
        return true;
    }

    void NoteRenderer::destroy()
    {
        for (NoteTrackRendererListIterator it=mNoteTrackRendererList.begin(); it!=mNoteTrackRendererList.end(); ++it)
        {
            delete (*it);
        }
        mNoteTrackRendererList.clear();
    }

    NoteRenderer::~NoteRenderer()
    {
        destroy();
    }
}
